// This sample illustrates how to properly log errors with all the
// available error information

var req = opensocial.newDataRequest();

// we're intentionally misspelling 'VIEWER' to induce an error
req.add(req.newFetchPersonRequest("VIEWR"),'myviewer');
req.add(req.newFetchPersonRequest("OWNER"),'myowner');
req.send(callback);

function callback(resp) {
   if (resp.hadError()) {
      handleError(resp, ['myviewer','myowner']);
      return;
   }
   debug.say('success');
}

function handleError(resp, items) {
   var tlmsg = resp.getErrorMessage();
   if (typeof(tlmsg) == 'undefined' || tlmsg == null) tlmsg = "";

   debug.say("*** Error: " + tlmsg);
   
   for (var i in items) {
      var item = resp.get(items[i]);
      var prefix = "***   "  + items[i] + ": ";

      if (typeof(item) == 'undefined' || item == 'null')
         debug.say(prefix + "[item unavailable]");
      else if (item.hadError())
         debug.say(prefix + "[" + item.getErrorCode() 
                          + "] " + item.getErrorMessage());
   }
}
